﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace StreakyDownloader.Model
{
   /// <summary>
   /// Interface implementation and wrapper for the standard system.net.webclient object.  
   /// </summary>
   public class StreakyWebClient : IWebClient
   {
      // NOTE: 
      // This wrapped object is what is used in the main application.  
      // The interface and wrapped object allow decoupling from the "closed source" 
      // framework web client object and opens up the possibility of testing the 
      // intereaction with a test double.

      /// <summary>
      /// the wrapped webclient member member
      /// </summary>
      protected WebClient m_webClient;

      /// <summary>
      /// event called when the asynchronously downloaded file has completed.
      /// </summary>
      public event System.ComponentModel.AsyncCompletedEventHandler DownloadFileCompleted;

      /// <summary>
      /// Event called when the asynchronously downloaded file has progress to report.
      /// </summary>
      public event System.Net.DownloadProgressChangedEventHandler DownloadProgressChanged;

      /// <summary>
      /// Default constructor to create the wrapped web client member.
      /// </summary>
      public StreakyWebClient()
      {
         m_webClient = new WebClient();
      }

      /// <summary>
      /// Wrapper for the web clients download data method.
      /// Downloads the resource at the specified uri as a byte array.
      /// </summary>
      /// <param name="url">location of the resource to download.</param>
      public byte[] DownloadData(string url)
      {
         return m_webClient.DownloadData(url);
      }

      /// <summary>
      /// Non blocking method for downloading the specified resource to a local file.
      /// The DownloadProgressChanged and DownloadFileComplete events are used to indicate
      /// download progress and completion respectively.
      /// </summary>
      /// <param name="address">The address of the resource to download.</param>
      /// <param name="localFilename">The desired name of the file after the download is complete.</param>
      public void DownloadFileAsync(Uri address, string localFilename)
      {
         m_webClient.DownloadFileAsync(address, localFilename);

         
      }

      public void CancelAsync()
      {
         m_webClient.CancelAsync();
      }
   }
}
